const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
  },

  onLoad(){
  },

  onShow(){
    this.getUserInfo();
  },

  onShareAppMessage(){

  },

  upload(){
    let _this = this;
    $wx.uploadSourceType([], res => {
      let temp_file_path = res.tempFilePaths;
      $wx.uploadFile({
        url: AJAX.uploadFile,
        filePath: temp_file_path,
        name: 'file',
        complete(res){
          let upload_res = JSON.parse(res.data);
          _this.setUserInfo({
            field: 'avatar',
            value: upload_res.data.url
          });
        }
      })
    });
  },

  /*获取用户信息*/
  getUserInfo() {
    AJAX.getUserInfo({
      loadType: 'bar',
      platform: 'wxapp'
    }).then(([status, res, msg]) => {
      if (status) {
        this.setData({
          userInfo: res.user,
          address: res.address
        });
      } else {
        $wx.showToast(msg);
      }
    });
  }, 

  /**
   * 设置用户信息
   * @param field 需要修改的字段
   * @param value 修改的值
   * */ 
  setUserInfo(params){
    let _this = this;
    params.loadType = 'default';
    params.loadMsg = '修改中...';
    AJAX.setUserInfo(params).then(([status, res, msg]) => {
      $wx.showToast(msg, {
        success(){
          if (status){
            _this.onShow();
          }
        }
      });
    });
  }
});